<?php

    class Session
    {

        const SESSION_NAME = "ispringsolutions.com";



        /**
         * Sets the value in the session
         *
         * @param $key string
         * @param $value string
         */
        public static function SetValue( $key, $value )
        {
            $isKey = ( isset( $key ) && !empty( $key ) );
            if ( !$isKey )
            {
                return;
            }

            if ( !isset( $value ) )
            {
                return;
            }

            self::StartSession();

            $_SESSION[$key] = $value;
        }

        /**
         * Gets value of session by key
         *
         * @param $key string
         * @param $defaultValue string
         * @return string
         */
        public static function GetValue( $key, $defaultValue = "" )
        {
            $isKey = ( isset( $key ) && !empty( $key ) );
            if ( !$isKey )
            {
                return $defaultValue;
            }

            self::StartSession();

            return Util::GetArrayCorrectString( $_SESSION, $key, $defaultValue );

        }

        /**
         * Gets value of session by key and clear value
         *
         * @param $key string
         * @param $defaultValue string
         * @return string
         */
        public static function GetAndClearValue( $key, $defaultValue = "" )
        {
            $value = self::GetValue( $key, $defaultValue );
            self::SetValue( $key, "" );
            return $value;
        }

        public static function RemoveKey($key)
        {
            self::StartSession();

            if (isset($_SESSION[$key]))
            {
                unset($_SESSION[$key]);
            }
        }

        public static function GetKeys()
        {
            self::StartSession();

            return array_keys($_SESSION);
        }

        /**
         * Opening session
         */
        private static function StartSession()
        {
            if ( session_id() == '' )
            {
                $sid = Request::GetParam('sid', '');
                if (!empty($sid))
                {
                    session_id($sid);
                }

                self::SetupSessionFilesPath();
                session_start();
                session_name( self::SESSION_NAME );
            }
        }

        private static function SetupSessionFilesPath()
        {
            if (Util::IsFreeBsdOs())
            {
                if (!defined('SESSION_FILES_PATH'))
                {
                    trigger_error("Your should specify SESSION_FILES_PATH constant.", E_ERROR);
                    return;
                }

                if (!file_exists(SESSION_FILES_PATH))
                {
                    Util::CreateFolderRecursively(SESSION_FILES_PATH);
                }

                ini_set('session.save_path', SESSION_FILES_PATH);
            }
        }
    }

?>